package string

// https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xne8id/
// 是否回文字符，只关注字母，忽略大小写
func IsPalindrome(s string) bool {
	start, end := 0, len(s)-1
	for {
		for start < end && !isLetterOrNumber(s[start]) {
			start++
		}
		for end > start && !isLetterOrNumber(s[end]) {
			end--
		}
		if start >= end {
			return true
		}
		if !isSameAlpha(s[start], s[end]) {
			return false
		}
		start++
		end--
	}
}

// 是否大小写字母或数字
func isLetterOrNumber(char uint8) bool {
	return (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9')
}

// 判断两个字符是否相等，忽略大小写
func isSameAlpha(a, b uint8) bool {
	if a == b {
		return true
	} else if a >= 'a' && a <= 'z' {
		a = a + 'A' - 'a'
	} else if a >= 'A' && a <= 'Z' {
		a = a + 'a' - 'A'
	}
	return a == b
}
